﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace JCFramework.Extensions
{
    public static class HtmlCodeHelper
    {
        private static string URL_HEAD = "http://";

        /// <summary>
        /// 根据某一个链接获取这个站点的url
        /// </summary>
        /// <param name="url">站点内的某个链接</param>
        /// <param name="hasHead">是否带http://</param>
        /// <returns></returns>
        /// <example>
        /// GetSiteUrlPath("http://www.taogame.com/em/eg/test.html", true)
        /// Return: http://www.taogame.com
        /// </example>
        public static string GetSiteUrlPath(string url, bool hasHead)
        {
            string result = String.Empty;

            if (url.StartsWith(URL_HEAD)) url = url.Remove(0, URL_HEAD.Length);
            url = url.Replace('\\', '/');
            if (url.IndexOf('/') < 0)
            {
                result = url;
            }
            else
            {
                result = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)[0];
            }

            return hasHead ? URL_HEAD + result : result;
        }

        /// <summary>
        /// 将相对地址转换为绝对地址
        /// </summary>
        /// <param name="primitiveUrl">要转换的相对地址</param>
        /// <param name="consultUrl">当前网页地址</param>
        /// <returns>转换后的绝对地址</returns>
        public static string DefiniteUrl(string primitiveUrl, string consultUrl)
        {
            if (String.IsNullOrEmpty(primitiveUrl) || String.IsNullOrEmpty(consultUrl))
            {
                throw new ArgumentNullException("Argument is Null");
            }

            string reUrl = "";

            //如果前置有http:// 手动删除
            if (consultUrl.StartsWith(URL_HEAD)) consultUrl = consultUrl.Remove(0, URL_HEAD.Length);


            consultUrl = consultUrl.Replace('\\', '/');
            //consultUrl = consultUrl.Replace("://", @":\\");
            if (consultUrl.IndexOf("/") < 0)
            {
                consultUrl = consultUrl + "/";
            }

            string[] arrayUrl = consultUrl.Split('/');

            if (primitiveUrl.StartsWith(URL_HEAD))
            {
                reUrl = primitiveUrl.Remove(0, URL_HEAD.Length);
            }
            else if (primitiveUrl.StartsWith("/"))
            {
                reUrl = arrayUrl[0] + primitiveUrl;
            }
            else if (primitiveUrl.StartsWith("../"))
            {
                int pi = 0;
                while (primitiveUrl.StartsWith("../"))
                {
                    pi += 1;
                    primitiveUrl = primitiveUrl.Remove(0, 3);
                }
                for (int i = 0; i < (arrayUrl.Length - 1 - pi); i++)
                {
                    if (reUrl != "")
                        reUrl = reUrl + arrayUrl[i] + "/";
                    else
                        reUrl = arrayUrl[i] + "/";
                }
                reUrl += primitiveUrl;
            }
            else
            {
                reUrl = consultUrl.Substring(0, consultUrl.LastIndexOf("/") + 1) + primitiveUrl;
            }

            return URL_HEAD + reUrl;
        }

        /// <summary>
        /// remove tag fram htmlcode
        /// </summary>
        /// <param name="strContent">html code</param>
        /// <param name="strRepCode">tag name</param>
        /// <returns>new htmlcode</returns>
        public static string RemoveTag(this string strContent, string strRepCode)
        {
            string strRegex = "(<" + strRepCode + ">)|(<" + strRepCode + "(.+?)>)";
            Regex reg = new Regex(strRegex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Regex reg0 = new Regex(@"</" + strRepCode + ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            strContent = reg.Replace(strContent, "");
            strContent = reg0.Replace(strContent, "");
            return strContent;
        }

        /// <summary>
        /// remove all tag
        /// </summary>
        /// <param name="strContent">html code</param>
        /// <returns>new html code</returns>
        public static string RemoveAllTag(this string strContent)
        {
            return RemoveTag(strContent, ".");
        }

    }
}
